package com.cskaoyan.gateway.controller.shopping;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.gateway.form.shopping.CartFrom;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.shopping.ICartService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.dto.AddCartRequest;
import com.mall.shopping.dto.AddCartResponse;
import com.mall.shopping.dto.UpdateCartNumRequest;
import com.mall.shopping.dto.UpdateCartNumResponse;
import org.apache.dubbo.config.annotation.Reference;
import com.mall.shopping.dto.*;
import com.mall.user.annotation.Anoymous;
import com.mall.user.intercepter.TokenIntercepter;
//import jdk.nashorn.internal.ir.annotations.Reference;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <h3>mall-parent</h3>
 *
 * @author:Zhao
 * @date:2021-11-01 15:59:08
 * @description:
 */
@Slf4j
@RestController
@RequestMapping("/shopping")
public class CartController {

    @Reference
    ICartService cartService;

    /**
     * @author Zhao
     * @date 2021/11/1 17:06
     * @description 添加商品到购物车，post请求
     */


    @PostMapping("carts")
    public ResponseData addToCart(@RequestBody CartFrom cartForm) {
        log.info("12345");

        AddCartRequest addCartRequest = new AddCartRequest();
        addCartRequest.setUserId(cartForm.getUserId());
        addCartRequest.setItemId(cartForm.getProductId());
        addCartRequest.setNum(cartForm.getProductNum());


        AddCartResponse addCartResponse = cartService.addToCart(addCartRequest);

        //校验response状态码
        if (!ShoppingRetCode.SUCCESS.getCode().equals(addCartResponse.getCode())) {
            return new ResponseUtil<>().setErrorMsg(addCartResponse.getMsg());
        }

        return new ResponseUtil<>().setData(null);
    }


    /**
     * @author Zhao
     * @date 2021/11/2 14:17
     * @description 修改购物车
     */
    @PutMapping("carts")
    public ResponseData updateCart(@RequestBody CartFrom cartForm) {
        log.info("12345");

        UpdateCartNumRequest request = new UpdateCartNumRequest();
        request.setChecked(cartForm.getChecked());
        request.setItemId(cartForm.getProductId());
        request.setNum(cartForm.getProductNum());
        request.setUserId(cartForm.getUserId());

        UpdateCartNumResponse response = cartService.updateCartNum(request);

        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            return new ResponseUtil().setData(response.getMsg());
        }

        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    /**
     * @author Zhao
     * @date 2021/11/2 15:09
     * @description 用按钮x删除购物车中的商品
     */
    @DeleteMapping("carts/{uid}/{pid}")
    public ResponseData DeleteCartItem(@PathVariable("uid") Long uid, @PathVariable("pid") Long pid) {
        log.info("12345");

        DeleteCartItemRequest request = new DeleteCartItemRequest();
        request.setUserId(uid);
        request.setItemId(pid);

        DeleteCartItemResponse response = cartService.deleteCartItem(request);

        return new ResponseUtil().setData(null);
    }
}
